package org.mespilus.mespilusweb.ui.usermanagement.client;

import java.util.ArrayList;
import java.util.List;

import org.mespilus.mespilusweb.ui.usermanagement.client.presenter.UserListPresenter;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.event.GridCellListener;

public class UserListPanel {
	protected UserListPresenter presenter;
	protected List<List<String>> data = new ArrayList<List<String>>();
	protected UserListPanelHandler userListPanelHandler = new UserListPanelHandler();
	protected RecordDef recordDef;
	protected MemoryProxy userDataProxy;
	protected ArrayReader userDataArrayReader;
	protected Store store;
	protected GridPanel usersGrid;
	protected Object[][] userData;

	public UserListPanel() {
	}
	
	public UserListPresenter getPresenter() {
		return presenter;
	}

	public void setPresenter(UserListPresenter presenter) {
		this.presenter = presenter;
	}

	public void buildPanel(){
		/*
		 * List<FieldDef> recordFields = new ArrayList<FieldDef>();
		 * recordFields.add(new StringFieldDef("mail")); recordFields.add(new
		 * StringFieldDef("name"));
		 * recordDef.createRecord(recordFields.toArray());
		 */
		recordDef = new RecordDef(new FieldDef[] { new StringFieldDef("mail"),
				new StringFieldDef("name") });
		MemoryProxy userDataProxy = new MemoryProxy(new Object[][]{});
		ArrayReader userDataArrayReader = new ArrayReader(recordDef);
		Store store = new Store(userDataProxy, userDataArrayReader);
		store.load();

		usersGrid = new GridPanel();
		usersGrid.setStore(store);
		ColumnConfig[] userGridColumns = new ColumnConfig[] {
				new ColumnConfig("Name", "name", 175, true),
				new ColumnConfig("Mail", "mail", 200, true) };
		ColumnModel columnModel = new ColumnModel(userGridColumns);
		usersGrid.setColumnModel(columnModel);
		usersGrid.setFrame(true);
		usersGrid.setStripeRows(true);
		// usersGrid.setAutoExpandColumn("mail");
		// usersGrid.setAutoExpandColumn("name");
		usersGrid.setTitle("User list");
		usersGrid.setHeight(450);
		usersGrid.setWidth(350);

		usersGrid.addGridCellListener(userListPanelHandler);
		presenter.getAsyncUserList();
	}
	
	public Panel getUserListPanel() {
		return usersGrid;
	}

	public class UserListPanelHandler implements GridCellListener {

		@Override
		public void onCellClick(GridPanel grid, int rowIndex, int colIndex,
				EventObject e) {
			Store gridStore = grid.getStore();
			Record record = gridStore.getAt(rowIndex);
			String email = record.getAsString("mail");
			presenter.userSelected(email);
		}

		@Override
		public void onCellContextMenu(GridPanel grid, int rowIndex,
				int cellIndex, EventObject e) {
		}

		@Override
		public void onCellDblClick(GridPanel grid, int rowIndex, int colIndex,
				EventObject e) {
		}

	}

	public void showResult(Object data[][]) {
		final MemoryProxy proxy = new MemoryProxy(data);
		final ArrayReader reader = new ArrayReader(recordDef);
		final Store temp = new Store(proxy, reader);
		temp.load();
		final Record rs[] = temp.getRecords();

		final Store store = usersGrid.getStore();
		store.removeAll();
		store.add(rs);
		store.commitChanges();
	}
}
